/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.utils;

import JFreeChartExtensions.chartextensions.crosshair.CrossHairExtension;
import JFreeChartExtensions.chartextensions.customsaveas.CustomSaveAsExtension;
import JFreeChartExtensions.chartextensions.customscale.CustomScaleExtension;
import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.containers.IChartPanelContainer;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChart;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChartException;
import JFreeChartExtensions.exceptions.ExtendableChartPanelException;
import JFreeChartExtensions.interfaces.IPluginProvider;
import java.awt.Dimension;
import java.awt.Point;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Nobody
 */
public class Inset extends InternalFrameWithChart implements InternalFrameListener {

    private JFreeChart smallChart;
    private ExtendableChartPanel smallPanel;
    private static Dimension defaultDimension = new java.awt.Dimension(100, 100);
    private static Point upperLeftCorner = new java.awt.Point(0, 0);

    public Inset(IChartPanelContainer container, JFreeChart chart, DatasetCollection datasetCollection) throws InternalFrameWithChartException {
        this(container, chart, defaultDimension, datasetCollection);
    }

    public Inset(IChartPanelContainer container, JFreeChart mainChart, Dimension dim, DatasetCollection collection) throws InternalFrameWithChartException {
        super(container.getParentFrame());
        setAutoscrolls(false);
        setClosable(false);
        setMaximizable(false);
        setIconifiable(false);
        setResizable(false);
        smallChart = ExtendableChartPanel.createXYLineChart(
                "",
                "",
                "",
                null,
                mainChart.getXYPlot().getOrientation(),
                false,
                false,
                false);

        smallPanel = new ExtendableChartPanel(smallChart, collection);
        smallPanel.setPreferredSize(dim);
        smallPanel.setBounds(0, 0, 20, 20);
        smallPanel.getChart().clearSubtitles();
        smallPanel.getChart().removeLegend();
        smallPanel.getChart().setBorderVisible(false);
        try {
            smallPanel.addExtension(new CustomScaleExtension(smallPanel));
            smallPanel.addExtension(new CrossHairExtension(smallPanel));
            smallPanel.addExtension(new CustomSaveAsExtension(smallPanel));

        } catch (ExtendableChartPanelException ex) {
            Logger.getLogger(Inset.class.getName()).log(Level.SEVERE, null, ex);
        }

        setBorder(null);
        setTitle("Inset Window");
        setResizable(true);
        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setAutoscrolls(true);
        setBounds(100, 100, 100, 100);
        setContentPane(smallPanel);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public ExtendableChartPanel getSmallPanel() {
        return smallPanel;
    }

    public Point getUpperLeftCorner() {
        return upperLeftCorner;
    }

    public ChartPanel getChartPanel() {
        return smallPanel;
    }

    public Dimension getDefaultDimension() {
        return defaultDimension;
    }

    @Override
    public void prepareChart(IPluginProvider pluginProvider) throws InternalFrameWithChartException {
    }

    public JFreeChart getSmallChart() {
        return smallChart;
    }

    @Override
    public void setPreferredSize(Dimension dim) {
        if (dim.height == 0 && dim.width == 0) {
            dim = (Dimension) defaultDimension.clone();
        }
        super.setPreferredSize(dim);
        smallPanel.setSize(dim);
    }

    @Override
    public void internalFrameOpened(InternalFrameEvent e) {
    }

    @Override
    public void internalFrameClosing(InternalFrameEvent e) {
    }

    @Override
    public void internalFrameClosed(InternalFrameEvent e) {
        try {
            setClosed(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(Inset.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void internalFrameIconified(InternalFrameEvent e) {
        setVisible(false);
    }

    @Override
    public void internalFrameDeiconified(InternalFrameEvent e) {
        setVisible(true);
    }

    @Override
    public void internalFrameActivated(InternalFrameEvent e) {
    }

    @Override
    public void internalFrameDeactivated(InternalFrameEvent e) {
    }
}
